package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.LmysdbgBsDetail;
import com.ruoyi.report_ljlm.domain.vo.LmysdbgBsDetailVo;
import com.ruoyi.report_ljlm.mapper.LmysdbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.LmysdbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.LmysdbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.LmysdbgBsInfo;
import com.ruoyi.report_ljlm.mapper.LmysdbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.ILmysdbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路面压实度试验检测报告(钻芯法)Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class LmysdbgBsInfoServiceImpl implements ILmysdbgBsInfoService {

    private final LmysdbgBsInfoMapper baseMapper;
    private final LmysdbgBsDetailMapper detailMapper;

    /**
     * 查询路面压实度试验检测报告(钻芯法)
     */
    @Override
    @FIllBasicInfo
    public LmysdbgBsInfoVo queryById(Long excelId) {
        LmysdbgBsInfoVo lmysdbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<LmysdbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<LmysdbgBsDetail>().eq(LmysdbgBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            lmysdbgBsInfoVo.setDetailList(detailList);
        }
        return lmysdbgBsInfoVo;
    }

    /**
     * 查询路面压实度试验检测报告(钻芯法)列表
     */
    @Override
    public TableDataInfo<LmysdbgBsInfoVo> queryPageList(LmysdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LmysdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<LmysdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路面压实度试验检测报告(钻芯法)列表
     */
    @Override
    public List<LmysdbgBsInfoVo> queryList(LmysdbgBsInfoBo bo) {
        LambdaQueryWrapper<LmysdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LmysdbgBsInfo> buildQueryWrapper(LmysdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LmysdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, LmysdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, LmysdbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), LmysdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), LmysdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), LmysdbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), LmysdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), LmysdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), LmysdbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), LmysdbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), LmysdbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), LmysdbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), LmysdbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), LmysdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), LmysdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), LmysdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqhhljplx()), LmysdbgBsInfo::getLiqhhljplx, bo.getLiqhhljplx());
        lqw.eq(bo.getBaozl() != null, LmysdbgBsInfo::getBaozl, bo.getBaozl());
        lqw.eq(bo.getLilxdmd() != null, LmysdbgBsInfo::getLilxdmd, bo.getLilxdmd());
        lqw.eq(bo.getLumbzxdmd() != null, LmysdbgBsInfo::getLumbzxdmd, bo.getLumbzxdmd());
        lqw.eq(bo.getLumbzmdycdsjz() != null, LmysdbgBsInfo::getLumbzmdycdsjz, bo.getLumbzmdycdsjz());
        lqw.eq(bo.getPingjz() != null, LmysdbgBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(bo.getDaibz() != null, LmysdbgBsInfo::getDaibz, bo.getDaibz());
        lqw.eq(bo.getBiaozc() != null, LmysdbgBsInfo::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getJiancds() != null, LmysdbgBsInfo::getJiancds, bo.getJiancds());
        lqw.eq(bo.getHegds() != null, LmysdbgBsInfo::getHegds, bo.getHegds());
        lqw.eq(bo.getHegl() != null, LmysdbgBsInfo::getHegl, bo.getHegl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), LmysdbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), LmysdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), LmysdbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), LmysdbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), LmysdbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), LmysdbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), LmysdbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), LmysdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), LmysdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), LmysdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路面压实度试验检测报告(钻芯法)
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(LmysdbgBsInfoBo bo) {
        LmysdbgBsInfo add = BeanUtil.toBean(bo, LmysdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<LmysdbgBsDetail> details = new ArrayList<>(15);
            for (int i = 1; i <= 15; i++) {
                details.add(new LmysdbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
            //报告

        }
        return flag;
    }

    /**
     * 修改路面压实度试验检测报告(钻芯法)
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(LmysdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        LmysdbgBsInfo update = BeanUtil.toBean(bo, LmysdbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), LmysdbgBsDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(LmysdbgBsInfoBo bo) {
        if (!BigDecimalUtil.isAnyOneEmpty(bo.getDaibz(), bo.getLumbzmdycdsjz())) {
            if (BigDecimalUtil.isLessOrEqual(bo.getDaibz(), bo.getLumbzmdycdsjz())) {
                bo.setExcelResult(2);
                return;
            }
        }

        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LmysdbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路面压实度试验检测报告(钻芯法)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public LmysdbgBsInfoVo jcjl(LmysdbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，所检指标均符合JTG F40-2004《公路沥青路面施工技术规范》，JTG F80/1-2017《公路工程质量检验评定标准》及设计文件要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
